<template>
  <div>
    <div>
      <el-button type="primary"
        icon="el-icon-plus"
        style="margin-bottom:10px"
        size="small"
        @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading"
      :data="dataList">
      <el-table-column label="功能名称"
        min-width="120"
        prop="name" />
      <el-table-column label="功能ID"
        min-width="120"
        prop="fn_id">
      </el-table-column>
      <el-table-column label="描述"
        min-width="120"
        prop="desc">
        <template slot-scope="scope">
          <div class="text-clamp2" :title="scope.row.desc">{{ scope.row.desc }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作"
        width="120"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="handleUpdate(scope.row)">修改</el-button>

          <el-button
            size="small"
            type="text"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList" />

    <el-dialog :title="dialogTitle"
      :visible.sync="open"
      width="650px"
      :close-on-click-modal="false"
      @closed="onDialogClosed">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
        <el-form-item label="功能名称" prop="name">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item label="功能ID" prop="fn_id">
          <el-input v-model.trim="form.fn_id" maxlenth="20"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model.trim="form.desc" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary"
          :loading="saveLoading"
          v-if="form.id"
          @click="submitForm(false)">确 认</el-button>
        <el-button type="primary"
          :loading="saveLoading"
          v-else
          @click="submitForm(true)">提交并继续添加</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getFnList, addFn, updateFn, deleteFn } from '@/api/addition'
export default {
  name: 'AdditionFnID',
  data () {
    return {
      loading: false,
      saveLoading: false,
      fnList: [],
      queryParams: {
        page: 1,
        page_size: 10
      },
      dataList: [],
      total: 0,
      open: false,
      form: {
        id: null,
        name: null,
        fn_id: null,
        desc: null
      },
      rules: {
        name: [{ required: true, message: '功能名称不能为空', trigger: 'blur' }],
        fn_id: [{ required: true, message: '功能ID不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    dialogTitle () {
      return this.form.id ? '修改' : '新增'
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleAdd () {
      this.open = true
    },
    getList () {
      this.loading = true
      getFnList(this.queryParams).then(res => {
        const data = res.data || {}
        this.dataList = data.list
        this.total = data.count
      }).finally(() => {
        this.loading = false
      })
    },
    handleUpdate (row) {
      this.open = true

      this.$nextTick(() => {
        Object.keys(this.form).forEach(key => {
          this.form[key] = row[key]
        })
      })
    },
    submitForm (isContinue) {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.saveLoading = true
          const fn = this.form.id ? updateFn : addFn
          fn(this.form).then(() => {
            this.getList()
            this.msgSuccess(this.form.id ? '修改成功' : `功能ID“${this.form.fn_id}”已成功添加！`)
            if (isContinue) {
              this.resetForm('form')
            } else {
              this.open = false
            }
          }).finally(() => {
            this.saveLoading = false
          })
        }
      })
    },
    onDialogClosed () {
      this.resetForm('form')
      this.form.id = null
    },
    /** 删除按钮操作 */
    handleDelete ({ id, fn_id }) {
      const { page, page_size } = this.queryParams
      this.$confirm('是否确认删除"' + fn_id + '"?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteFn({ id }).then(() => {
          this.queryParams.page = this.getCurrentPage({ total: this.total, pageSize: page_size, pageIndex: page })
          this.getList()
          this.msgSuccess(`功能ID“${fn_id}”已删除！`)
        })
      })
    }
  }
}
</script>
